<?php

namespace addons\mycurrency\controller\job;

use app\common\model\mycurrency\AgentMoneyWithdrawal;
use think\Db;
use think\Exception;
use think\Log;
use think\Queue;
use think\queue\Job;
use addons\mycurrency\library\WechatTransfer;

class Payment {

    /**
     * 打款进度查询
     * @param Job $job
     * @param $data
     * @throws \think\exception\DbException
     */
    public function cashwithdrawal(Job $job, $data) {
        if ($job->attempts() > 3) {
            $job->delete();
        }
        $payment_id = $data['payment_id'];
        $payment = \app\common\model\mycurrency\Payment::get($payment_id);
        if($payment){
            $wechat_interface_return = json_decode($payment['wechat_interface_return'], true);
            $wechatPay = new WechatTransfer();
            $result = $wechatPay->transferBatchesDetail($wechat_interface_return['out_batch_no'], $wechat_interface_return['out_detail_no']);
            if ($result['code'] == 0) {
                $data = $result['data'];
                if ($data->detail_status == 'INIT' || $data->detail_status == 'WAIT_PAY' || $data->detail_status == 'PROCESSING') {//转账中
                    $queue_default = config('queue.default');
                    Queue::later(60, "addons\mycurrency\controller\job\Payment\cashwithdrawal", ['payment_id' => $payment['id']], $queue_default);
                } else if ($data->detail_status == 'SUCCESS') {//转账成功
                    $payment->state = \app\common\model\mycurrency\Payment::STATE_YIDAKUAN;
                    $payment->successtime = time();
                    $payment->save();
                    if($payment['withdrawal_type'] == \app\common\model\mycurrency\Payment::WIHDRAWAL_TYPE_DAILISHANGTIXIAN){//提现类型:代理商收益提现
                        $withdrawal = AgentMoneyWithdrawal::get($payment['agent_money_withdrawal_id']);
                        if($withdrawal){
                            $withdrawal->status = AgentMoneyWithdrawal::STATUS_YIDAKUAN;
                            $withdrawal->paymenttime = time();
                            $withdrawal->save();
                        }
                    }
                } else if ($data->detail_status == 'FAIL') {//转账失败
                    $payment->state = \app\common\model\mycurrency\Payment::STATE_SHIBAI;
                    $payment->fail_reason = $result['data']['msg'];
                    $payment->failtime = time();
                    $payment->save();

                    if($payment['withdrawal_type'] == \app\common\model\mycurrency\Payment::WIHDRAWAL_TYPE_DAILISHANGTIXIAN){//提现类型:代理商收益提现
                        $withdrawal = AgentMoneyWithdrawal::get($payment['agent_money_withdrawal_id']);
                        if($withdrawal){
                            $withdrawal->status = AgentMoneyWithdrawal::STATUS_SHENHETONGGUO;
                            $withdrawal->save();
                        }
                    }
                }
            }
        }
        $job->delete();
    }
}